package profitshare

import (
	. "wxsdk/wxpay"
	// "errors"
	"wxsdk/wxpay/core"
	"wxsdk/wxpay/utils/mapstruct"
	//"github.com/astaxie/beego"
)

type Receiver struct {
	Type           string `json:"type"`                      //
	Account        string `json:"account"`                   //
	Name           string `json:"name,omitempty"`            //
	RelationType   string `json:"relation_type"`             //
	CustomRelation string `json:"custom_relation,omitempty"` //
}

type AddReceiveReq struct {
	Appid    string `xml:"appid"`                //微信分配的小程序ID
	MchId    string `xml:"mch_id"`               //mch_id
	SubAppid string `xml:"sub_appid,omitempty"`  //小程序的APPID,当前调起支付的小程序APPID(服务商特有参数)
	SubMchId string `xml:"sub_mch_id,omitempty"` //微信支付分配的子商户号(服务商特有参数)
	NonceStr string `xml:"nonce_str"`            //随机字符串
	Sign     string `xml:"sign"`                 //签名
	SignType string `xml:"sign_type,omitempty"`  //签名类型
	Receiver string `xml:"receiver"`             //分账接收方列表
}

//
type AddReceiveRsp struct {
	BaseResponse `xml:",innerXml,omitempty"`
	Receiver     string `xml:"receiver"`
}

type OrderQueryResponse *AddReceiveRsp

func AddReceive(appId, mchId, subAppid, subMchId, appSecret, receiver string) (OrderQueryResponse, error) {
	var (
		req           = new(AddReceiveReq)
		rsp           = new(AddReceiveRsp)
		ReqUrl string = "https://api.mch.weixin.qq.com/pay/profitsharingaddreceiver"
	)
	client := core.GetWxPayClient(appId, mchId, appSecret, nil)
	req.Appid = appId
	if len(subAppid) > 0 {
		req.SubAppid = subAppid
	}
	req.MchId = mchId
	if len(subMchId) > 0 {
		req.SubMchId = subMchId
	}
	req.Receiver = receiver
	rspMap, err := client.ExcuteMap(ReqUrl, req)
	if err != nil {
		return rsp, err
	}
	err = mapstruct.Map2Struct(rspMap, rsp, "xml")
	if err != nil {
		return rsp, err
	}
	return rsp, nil
}
